package com.neuedu.weishi.controller;

import com.neuedu.weishi.entity.Member;
import com.neuedu.weishi.entity.Video;
import com.neuedu.weishi.entity.VideoCategory;
import com.neuedu.weishi.service.IMemberService;
import com.neuedu.weishi.service.IVideoCategoryService;
import com.neuedu.weishi.service.IVideoService;
import com.neuedu.weishi.vo.ResponseData;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

/**
 * Author lu
 * Date   2025-08-26 09:11:00
 */
@Log4j2
@RequestMapping("/weishi/video/api")
@RestController
@Tag(name = "视频控制器")
public class VideoController {

    @Autowired
    @Qualifier("videoService")
    private IVideoService videoService;

    @Autowired
    @Qualifier("videoCategoryService")
    private IVideoCategoryService videoCategoryService;

    @Autowired
    @Qualifier("memberService")
    private IMemberService memberService;

    /**
     * 发布视频
     *
     * @param video
     * @return
     */
    @PostMapping("release")
    @Operation(summary = "发布视频")
    public ResponseData releaseVideo(Video video) {
        boolean result = videoService.save(video);
        return ResponseData.success(result);
    }

    /**
     * 分页查询视频
     *
     * @param current 当前页
     * @param size    每页条数
     * @param keyword 关键词
     * @param video   视频(封装查询条件)
     * @return 视频分页列表
     */
    @GetMapping("/get_page_list")
    @Operation(summary = "分页查询视频")
    @Parameters({
            @Parameter(name = "current", description = "当前页", required = false),
            @Parameter(name = "size", description = "每页条数", required = false),
            @Parameter(name = "keyword", description = "关键词", required = false),
            @Parameter(name = "memberId", description = "发布视频会员ID", required = false),
            @Parameter(name = "categoryId", description = "视频分类ID", required = false),
    })
    public ResponseData selectPageList(@RequestParam(defaultValue = "1") int current,
                                       @RequestParam(defaultValue = "10") int size,
                                       @RequestParam(required = false) String keyword, @Parameter(hidden = true) Video video) {
        return ResponseData.success(videoService.selectPageList(current, size, keyword, video));
    }

    /**
     * 根据ID查询视频
     * @param videoId
     * @return
     */
    @GetMapping("/get_by_id/{id}")
    @Operation(summary = "根据ID查询视频")
    @Parameters({
            @Parameter(name = "id", description = "视频ID", required = true),
    })
    public ResponseData getById(@PathVariable("id") int videoId) {
        Video video = videoService.getById(videoId);
        if(video==null){
            return ResponseData.fail(999,"视频不存在");
        }
        int reviewStatus = video.getReviewStatus();
        if(2!=reviewStatus){
            return ResponseData.fail(999,"视频审核中,请稍后查看");
        }
        int videoStatus = video.getVideoStatus();
        if(2!=videoStatus){
            return ResponseData.fail(999,"该视频已被下架");
        }
        Member member = memberService.getById(video.getMemberId());
        video.setMember(member);
        VideoCategory category=videoCategoryService.getById(video.getCategoryId());
        video.setCategory(category);
        return ResponseData.success(video);
    }
}